/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package flightlifter;

import java.nio.FloatBuffer;
import java.util.Vector;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author Matt
 */
public class Cannon extends Node implements WiringConsumer {

    Vector<Node> nodes;
    Vector<Node> toAdd;
    int cnt = 0;
    boolean trigger = false;
    float lbrot;
    OrthoNormalBasis ONB;
    MeshLoader meshLoader;
    private float brot = 0, dbrot = 0;

    public Cannon(OrthoNormalBasis ONB, MeshLoader meshLoader, Vector<Node> nodes, Vector<Node> toAdd) {
        this.toAdd = toAdd;
        this.nodes = nodes;
        this.ONB = ONB;
        this.meshLoader = meshLoader;
    }

    @Override
    public void physics() {
        if (trigger) {
            if (dbrot < 30){
                dbrot += .4f;
            }
   

            
            if ((brot-lbrot) > 60) {
                lbrot = brot;
                toAdd.add(new Bullet(ONB.front.pos.get(), ONB.front.lastPos.get(), ONB.middle.pos.get()));
                trigger = false;
                cnt = 0;

                PVector dir = ONB.getForward();
                dir.normalize();
                dir.mult(-10f);
                //reverse impulse
                //a.impulse(dir);
                //b.impulse(dir);

            }
        } else {
            //gun spinning down
            dbrot *= .99f;
        }
        brot += dbrot;

    }

    @Override
    public void render(int mode) {



        if (selected == 1) {
            GL11.glColor3f(1.0f, 1.0f, 0.0f);
        } else {
            GL11.glColor3f(.75f, 0.75f, 0.75f);
        }



        GL11.glPushMatrix();
        PVector rot = ONB.getForwardCentroid();
        GL11.glTranslatef(rot.x, rot.y, rot.z);
        GL11.glColor4f(1, 1, 1, 1f);
        //GL11.glScalef(1,1,2);


        //I don't understand the code below.
        //it was a shot in the dark from
        //staring at the gluLookat code.
        //matrices are scary...it works

        //here be dragons



        PVector u = ONB.getUp();
        u.normalize();

        PVector r = ONB.getRight();
        r.normalize();

        PVector f = ONB.getForward();
        f.normalize();

        float[] temp = {r.x, r.y, r.z, 0,
            u.x, u.y, u.z, 0,
            f.x, f.y, f.z, 0,
            0, 0, 0, 1.f};

        FloatBuffer model = FlightLifter.allocFloats(temp);

        GL11.glMultMatrix(model);
        GL11.glRotatef(brot, 0, 0, 1);
        GL11.glScalef(2f, 2f,2f);
        meshLoader.draw();

        GL11.glPopMatrix();
        // exhaust.render();

    }

    public Cannon(String[] line, Vector<Node> nodes, Vector<Node> toAdd, MeshLoader meshLoader) {
        this(new OrthoNormalBasis((Point) nodes.elementAt(Integer.parseInt(line[1])),
                (Point) nodes.elementAt(Integer.parseInt(line[2])),
                (Point) nodes.elementAt(Integer.parseInt(line[3]))),
                meshLoader,
                nodes,
                toAdd);

    }

    @Override
    public String pickle(Vector<Node> nodes) {
        return "K,"
                + ONB.pickle(nodes);
    }

    @Override
    public PVector getCentroid() {
        return ONB.getCentroid();


    }

    @Override
    public PVector getScreenSpaceCentroid() {
        PVector tmp = getCentroid();
        float[] ret = GLProject.project(tmp.x, tmp.y, tmp.z);

        return new PVector(ret[0], ret[1], 0);

    }

    @Override
    public void setValue(float value) {
        if (value > .9f) {
            trigger = true;
        }
    }

    @Override
    public PVector getScreenCenter() {
        return getScreenSpaceCentroid();
    }

    //TODO need ONB depends!
    public boolean dependsOn(Point p) {
        return false;
    }
}
